package org.dawning.shop.product.rest;

import lombok.extern.log4j.Log4j;
import org.axonframework.commandhandling.CommandCallback;
import org.axonframework.commandhandling.CommandMessage;
import org.axonframework.commandhandling.gateway.CommandGateway;
import org.dawning.shop.common.commands.product.CreateProductCommand;
import org.dawning.shop.common.domain.product.Product;
import org.dawning.shop.common.domain.product.ProductId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by miou on 2017/6/15 0015.
 */
@RestController
@RequestMapping(value = "/product")
@Log4j
public class ProductDomainController {

    @Autowired
    private CommandGateway commandGateway;

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String add(@RequestBody Product product) {
        commandGateway.send(new CreateProductCommand(new ProductId().getIdentifier(),product),new CommandCallback(){

            @Override
            public void onSuccess(CommandMessage commandMessage, Object result) {
                log.info("success");
            }

            @Override
            public void onFailure(CommandMessage commandMessage, Throwable cause) {
                log.info("error");
            }
        });
        return "{\"code\":\"200\"}";
    }

}




